export const toast = (msg, type = 'none') => {
  let option = {
    title: msg,
    icon: type,
    duration: 1500,
    mask: true,
    fail: function (err) {
      console.error(err)
    }
  }
  wx.showToast(option)
}

export const loading = {
  show: () => {
    wx.showLoading({
      title: '',
      mask: true,
      fail: function (err) {
        console.error(err)
      }
    })
  },
  close: () => {
    wx.hideLoading()
  }
}

export const dialog = (msg, title = '提示', confirmColor = '#5d93fd', cancelColor = '#000000', cancelText = '取消', confirmText = '确定') =>
  new Promise((resolve, reject) => {
    wx.showModal({
      title,
      content: msg,
      showCancel: true,
      cancelText: cancelText,
      confirmText: confirmText,
      confirmColor: confirmColor,
      cancelColor: cancelColor,
      success: function (res) {
        if (res.confirm) {
          resolve('confirm')
        } else if (res.cancel) {
          reject(new Error('cancel'))
        }
      },
      fail: function (err) {
        console.error(err)
      }
    })
  })

export const alert = (msg, title = '提示') =>
  new Promise((resolve, reject) => {
    wx.showModal({
      title,
      content: msg,
      showCancel: false,
      confirmText: '确定',
      confirmColor: '#5d93fd',
      success: function (res) {
        if (res.confirm) {
          resolve('confirm')
        }
      },
      fail: function (err) {
        console.error(err)
      }
    })
  })

export const cookie = {
  set: (key, data) => {
    try {
      wx.setStorageSync(key, data)
    } catch (error) {
      console.error(error)
    }
  },
  get: key => {
    try {
      let value = wx.getStorageSync(key)
      return value
    } catch (error) {
      console.error(error)
    }
  },
  remove: key => {
    try {
      wx.removeStorageSync(key)
    } catch (error) {
      console.error(error)
    }
  },
  clear: () => {
    try {
      wx.clearStorageSync()
    } catch (error) {
      console.error(error)
    }
  }
}

export const getCurrentPageParams = name => {
  // eslint-disable-next-line
  let pages = getCurrentPages()
  let currentPage = pages[pages.length - 1]
  let options = currentPage.options

  return options[name]
}

export const base64ToSrc = base64 =>
  new Promise((resolve, reject) => {
    const [, format, bodyData] =
    /data:image\/(\w+);base64,(.*)/.exec(base64) || []
    if (!format) {
      reject(new Error('ERROR_BASE64_DATA'))
    }
    const timestamp = new Date().valueOf()
    const filePath = `${wx.env.USER_DATA_PATH}/qr_${timestamp}.${format}`
    wx.getFileSystemManager().writeFile({
      filePath,
      data: bodyData,
      encoding: 'base64',
      success () {
        resolve(filePath)
      },
      fail () {
        reject(new Error('ERROR_IN_WRITE_FILE'))
      }
    })
  })

/**
 * 格式化货币(默认人民币元)
 *
 * @param {Number} money 金额
 * @param {Number} ratio 换算比例
 * @param {String} currency 货币符号
 */
export const currencyFormatter = (money = 0, ratio = 1, currency = '￥ ') => {
  let moneyStr = currency + (money * ratio).toFixed(2)
  return moneyStr
}
